package com.msb.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.msb.mall.ware.vo.PurchaseDoneVO;
import com.msb.mall.ware.vo.PurchaseMergeVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.msb.mall.ware.entity.PurchaseEntity;
import com.msb.mall.ware.service.PurchaseService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;



/**
 * 采购信息
 *
 * @author dpb
 * @email dengpbs@163.com
 * @date 2022-10-08 23:00:46
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 完成采购
     * {
     *     id:采购单
     *     items:[{itemid:1,status:4,reson:""},{},...]
     * }
     * @return
     */
    @RequestMapping("/done")
    public R done(@RequestBody PurchaseDoneVO vo){
        purchaseService.done(vo);
        return R.ok();
    }

    /**
     * 领取采购单
     * @return
     */
    @RequestMapping("/receive")
    public R receivePurchase(@RequestBody List<Long> ids){
        int i = purchaseService.receivePurchase(ids);
        if (i == -1){
            return R.error("没有可领取的采购需求...");
        }
        return R.ok();
    }

    /**
     * 合并整单，采购需求合并为采购单
     * @return
     */
    @RequestMapping("/merge")
    public R mergePurchase(@RequestBody PurchaseMergeVO vo){
        int merge = purchaseService.merge(vo);
        if (merge == -1){
            return R.error("合并失败...该采购需求已经合单");
        }
        return R.ok();
    }

    /**
     * 合并整单 - 获取采购单 状态为 新建/已分配
     * @return
     */
    @RequestMapping("/unreceive/list")
    public R queryPageUnreceive(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPageUnreceive(params);
        return R.ok().put("page",page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase){
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
